package org.senseframework.support.orm.mybatis.definition;

import org.apache.commons.lang3.StringUtils;
import org.senseframework.support.orm.mybatis.query.builder.QueryBuilder;

import java.util.List;

/**
 * Author lanjian
 * Email  jianlan@iflytek.com
 */
public class EntityColumnDefinition {
    private String column;
    private String javaColumn;
    private Class type;
    private String tableAs;
    private String columnAs;
    private String length;
    private Boolean id;
    private String desc;
    private boolean nullable;
    private String generated;

    public String getGenerated() {
        return generated;
    }

    public void setGenerated(String generated) {
        this.generated = generated;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public boolean isNullable() {
        return nullable;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    public Boolean getId() {
        return id;
    }

    public void setId(Boolean id) {
        this.id = id;
    }

    public String column() {
        if (StringUtils.isBlank(tableAs)) {
            if (StringUtils.isBlank(columnAs)) {
                return column + " as " + column;
            }
            return column + " as " + columnAs;
        } else {
            if (StringUtils.isBlank(columnAs)) {
                return tableAs + "." + column + " as " + column;
            }
            return tableAs + "." + column + " as " + columnAs;
        }
    }

    public String columnNoAs() {
        if (StringUtils.isBlank(tableAs)) {
            return column;
        } else {
            return tableAs + "." + column;
        }
    }


    public void inSql(StringBuffer stringBuffer, QueryBuilder queryBuilder, Query query) {
        List list = (List) query.getValue();
        stringBuffer.append("(");
        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            stringBuffer.append("#{" + query.getPropertyAs() + i + "}");
            if (i != list.size() - 1) {
                stringBuffer.append(",");
            }
            queryBuilder.put(query.getPropertyAs() + i, o);
        }
        stringBuffer.append(")");
    }

    public String whereIn(Query query, QueryBuilder queryBuilder) {
        StringBuffer stringBuffer = new StringBuffer(columnNoAs() + query.getWhere().getDesc());
        inSql(stringBuffer, queryBuilder, query);
        return stringBuffer.toString();
    }

    public String whereBetween(Query query, QueryBuilder queryBuilder) {
        Object value=query.getValue();
        if(value instanceof Between){
            Between bV= (Between) value;
            StringBuffer stringBuffer = new StringBuffer("(");
            stringBuffer.append(columnNoAs() + query.getWhere().getDesc());
            stringBuffer.append("#{" + query.getPropertyAs() + "0}");
            queryBuilder.put(query.getPropertyAs()+"0",bV.getS());
            stringBuffer.append(" AND ");
            stringBuffer.append("#{" + query.getPropertyAs() + "1}");
            queryBuilder.put(query.getPropertyAs()+"1",bV.getE());
            stringBuffer.append(")");
            return stringBuffer.toString();
        }else{
            throw new RuntimeException("this query not between");
        }
    }

    public String where(Query query, QueryBuilder queryBuilder) {
        //当做map处理
        queryBuilder.put(query.getPropertyAs(), query.getValue());
        return columnNoAs() + query.getWhere().getDesc() + "#{" + query.getPropertyAs() + "}";
    }

    public String whereSingle(Query query) {
        return columnNoAs() + query.getWhere().getDesc();
    }

    public String set(String propertyAs) {
        return columnNoAs() + "=" + "#{" + propertyAs + "}";
    }

    public String orderBy(Order order) {
        return columnNoAs() + " " + order.getOrderBy().name();
    }

    public String getJavaColumn() {
        return javaColumn;
    }

    public void setJavaColumn(String javaColumn) {
        this.javaColumn = javaColumn;
    }

    public String getTableAs() {
        return tableAs;
    }

    public void setTableAs(String tableAs) {
        this.tableAs = tableAs;
    }

    public String getColumnAs() {
        return columnAs;
    }

    public void setColumnAs(String columnAs) {
        this.columnAs = columnAs;
    }

    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public Class getType() {
        return type;
    }

    public void setType(Class type) {
        this.type = type;
    }


}
